package com.cartgo.controller.main
{
	import com.cartgo.controller.BaseController;
	import com.cartgo.manager.UserManager;
	import com.cartgo.pmodel.main.MainViewPModel;
	import com.cartgo.services.ITestDelegates;
	import com.cartgo.vo.services.VOTableList;
	import com.cartgo.vo.tests.VOTestCrud;
	import com.cartgo.vo.users.VOUserSession;
	
	import flash.events.IEventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.utils.services.ServiceHelper;

	public class MainViewController extends BaseController
	{
		[Bindable]
		[Inject]
		public var model:MainViewPModel;
		
		[Inject]
		public var sh:ServiceHelper;
		
		[Inject]
		public var delegates:ITestDelegates;
		
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		public var userManager:UserManager = UserManager.getInstance();
		
		public function MainViewController()
		{
		}
		
		public function testTableList():void 
		{
			sh.executeServiceCall(delegates.testTableList(model.tableList), onTableListResult);
		}
		
		public function onTableListResult(evt:ResultEvent):void 
		{
			if(!checkRemoteError(evt)){
				var result:VOTableList = evt.result as VOTableList;
				model.tableList = result;
				model.tableListData = new ArrayCollection(result.data as Array);
			}
		}
		
		public function ongridclick(item:Object):void {
	        var selectedItem:VOTestCrud = item as VOTestCrud;
			model.crud.id = selectedItem.id;
			model.crud.label = selectedItem.label;
			model.crud.value = selectedItem.value;
		}
		
		public function testSave():void {
			sh.executeServiceCall(delegates.testSave(model.crud), onTestSaveResult);
		}
		
		public function onTestSaveResult(evt:ResultEvent):void {
			if(!checkRemoteError(evt)){
				testTableList();
			}
		}
	}
}